package demo.lintcode.linklist;

import demo.practice.ListNode;

public class P167 {

    public static void main(String[] args) {
        P167 p167= new P167();
        ListNode li= new ListNode(1);
    }

    public ListNode addLists(ListNode l1, ListNode l2) {

        ListNode next = childLists(l1, l2);

        if (next.val > 10) {
            ListNode nnext = next.next;
            next = new ListNode(next.val - 10);
            next.next = nnext;
            ListNode root = new ListNode(1);
            root.next = next;
            return root;
        }

        return next;

    }


    public ListNode childLists(ListNode l1, ListNode l2) {
        if (l1.next != null) {
            ListNode next = childLists(l1.next, l2.next);
            int sum = l1.val + l2.val;
            if (next != null) {
                if (next.val > 10) {
                    ListNode nnext = next.next;
                    next = new ListNode(next.val - 10);
                    next.next = nnext;
                    sum++;
                }
            }

            ListNode cur = new ListNode(sum);
            cur.next = next;
            return cur;
        } else {
            return null;
        }
    }


}
